home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / bother__ / cenvid.zip / CENVIDOS.ZIP / WININI.LIB < prev    next >
Text File  |  1995-03-28  |  7KB  |  203 lines

  1. // WinIni.lib - Cmm routines to interface with Windows' Profile
  2. // ver.2        (i.e. *.INI) files from DOS.
  3. //
  4. // NOTE: This should not be called from CEnviD for Windows
  5. //
  6. //**** GetIniString(): return key value from .ini file
  7. // SYNTAX: string GetIniString(string FileName,string AppName,string KeyName)
  8. // WHERE: FileName: name of profile (*.INI) file
  9. //        AppName: Application name ([AppName] in .ini files)
  10. //        KeyName: Key name under [AppName] in the .ini file
  11. // RETURN: Return the profile string if found; else return NULL if not
  12. //         found or if file cannot be read
  13. //
  14. //
  15. //**** WriteIniString(): write key value to .ini file
  16. // SYNTAX: bool WriteIniString(string FileName,string AppName,string KeyName,
  17. //                             string Value[,bool Replace])
  18. // WHERE: FileName: name of profile file
  19. //        AppName: Application name ([AppName] in .ini files)
  20. //        KeyName: Key name under [AppName] in the .ini file; if NULL then
  21. //                 deletes the entire application section
  22. //        Value: String to set KeyName equal to in the .INI file; if NULL
  23. //               then delete this key
  24. //        Replace: Optional (True if not supplied) to Replace any existing key
  25. //                 with this value, else replaces the key with this value
  26. // RETURN: Return FALSE if failure, else success
  27. //
  28. //
  29. //**** GetIniAppList(): Get array of all Application names in a profile file
  30. // SYNTAX: string[] GetIniAppList(string FileName)
  31. // WHERE: FileName: name of profile file
  32. // RETURN: Returns array of all AppNames in ini file; NULL if none found
  33. //
  34. //
  35. //**** GetIniKeyList(): Get array of all keys in a profile file for Application
  36. // SYNTAX: string[] GetProfileKeyList(string FileName,string AppName)
  37. // WHERE: FileName: name of profile file
  38. //        AppName: Application name ([AppName] in .ini files)
  39. // RETURN: Returns array of all keys under AppName; NULL if none found
  40. //           Each element of array has following two values:
  41. //                .Key     KeyName string
  42. //                .Value   Value string
  43. //
  44. //
  45.  
  46.  
  47. GetIniString(pFileName,pAppName,pKeyName)
  48. {
  49.    if ( lList = GetIniKeyList(pFileName,pAppName) ) {
  50.       lKeyCount = 1 + GetArraySpan(lList);
  51.       for ( lIdx = 0; lIdx < lKeyCount; lIdx++ ) {
  52.          if ( !stricmp(pKeyName,lList[lIdx].Key) )
  53.             return lList[lIdx].Value;
  54.       }
  55.    }
  56.    return NULL;
  57. }
  58.  
  59. WriteIniString(pFileName,pAppName,pKeyName,pValue,pReplace)
  60. {
  61.    lAppNameLen = strlen(pAppName);
  62.  
  63.    // read file
  64.    if ( !ReadIniFile(pFileName,lIniList) )
  65.       return False;
  66.  
  67.    // locate the section in the IniFile for pAppName
  68.    lAppMax = GetArraySpan(lIniList);
  69.    for ( lAppIdx = 1; lAppIdx <= lAppMax; lAppIdx++ ) {
  70.       if ( !strnicmp(pAppName,lIniList[lAppIdx].App,lAppNameLen) )
  71.          break;
  72.    }
  73.    if ( lAppIdx <= lAppMax ) {
  74.       if ( !pKeyName ) {
  75.          // pKeyName is NULL, and so delete this section
  76.          undefine( lIniList[lAppIdx].Key );
  77.       } else {
  78.          lKeyNameLen = sprintf(lKeyName,"%s=",pKeyName);
  79.          // application section was found; if replace and found then overwrite
  80.          // else write at the end.
  81.          lKeyMax = GetArraySpan(lKeys = lIniList[lAppIdx].Key);
  82.          lReplaced = (pValue == NULL);
  83.          if ( va_arg() < 5  ||  pReplace ) {
  84.             for ( lIdx = 1; lIdx <= lKeyMax; lIdx++ ) {
  85.                if ( !strnicmp(lKeyName,lKeys[lIdx],lKeyNameLen) ) {
  86.                   if ( !pValue ) {
  87.                      // delete this key
  88.                      lKeys[lIdx] = NULL;
  89.                   } else {
  90.                      sprintf(lKeys[lIdx],"%s=%s\n",pKeyName,pValue);
  91.                      lReplaced = True;
  92.                   }
  93.                }
  94.             }
  95.          }
  96.          if ( !lReplaced ) {
  97.             // add this value to the end of keys
  98.             lLast = lKeys[lKeyMax];
  99.             if ( lLast[0] && lLast[0] != '\n' )
  100.                lLast = lKeys[++lKeyMax];
  101.             sprintf(lLast,"%s=%s\n",pKeyName,pValue);
  102.          }
  103.       }
  104.    } else {
  105.       // application section was not found, so add a new application section
  106.       strcpy(lIniList[lAppMax+1].App,pAppName);
  107.       sprintf(lIniList[lAppMax+1].Key[0],"[%s]\n",pAppName);
  108.       sprintf(lIniList[lAppMax+1].Key[1],"%s=%s\n",pKeyName,pValue);
  109.    }
  110.    return WriteIniFile(pFileName,lIniList);
  111. }
  112.  
  113.  
  114. GetIniAppList(pFileName)
  115. {
  116.    if ( !ReadIniFile(pFileName,lIniList) )
  117.       return NULL;
  118.    if ( (lAppMax = GetArraySpan(lIniList)) < 1 )
  119.       return NULL;
  120.    for ( lIdx = 1; lIdx <= lAppMax; lIdx++ )
  121.       strcpy(lList[lIdx-1],lIniList[lIdx].App);
  122.    return lList;
  123. }
  124.  
  125. GetIniKeyList(pFileName,pAppName)
  126. {
  127.    if ( !ReadIniFile(pFileName,lIniList) )
  128.       return NULL;
  129.    if ( (lAppMax = GetArraySpan(lIniList)) < 1 )
  130.       return NULL;
  131.    for ( lIdx = 1; lIdx <= lAppMax; lIdx++ ) {
  132.       if ( !stricmp(pAppName,lIniList[lIdx].App) )
  133.          break;
  134.    }
  135.    if ( lIdx <= lAppMax ) {
  136.       lKeyList = lIniList[lIdx].Key;
  137.       lCount = 0;
  138.       lKeyMax = GetArraySpan(lKeyList);
  139.       for ( lIdx = 1; lIdx <= lKeyMax; lIdx++ ) {
  140.          lString = lKeyList[lIdx];
  141.          if ( lString[0] != ' ' && lString[0] != ';'  && (lEqual = strchr(lString,'=')) ) {
  142.             if ( lLen = lEqual - lString ) {
  143.                strncpy(lList[lCount].Key,lString,lLen+1);
  144.                lList[lCount].Key[lLen] = '\0';
  145.                strcpy(lValue,lEqual+1);
  146.                if ( lValue[strlen(lValue)-1] == '\n' )
  147.                   lValue[strlen(lValue)-1] = '\0';
  148.                strcpy(lList[lCount].Value,lValue);
  149.                lCount++;
  150.             }
  151.          }
  152.       }
  153.       if ( !lCount )
  154.          return NULL;
  155.    }
  156.    return lList;
  157. }
  158.  
  159. ReadIniFile(pFileName,pIniList)  // set ini array, return False if cannot
  160. {                                // read file
  161.    undefine(pIniList); // undo any previos settings
  162.    if ( !(lFp = fopen(pFileName,"rt")) )
  163.       return False;
  164.    // pretend initial app is blank
  165.    pIniList[lAppIdx = 0].App = "";
  166.    lKeyIdx = -1;
  167.    while ( lLine = fgets(lFp) ) {
  168.       if ( lLine[0] == '['  &&  strchr(lLine+1,']') ) {
  169.          // this is a new application name
  170.          lEnd = strchr(strcpy(pIniList[++lAppIdx].App,lLine+1),']');
  171.          lEnd[0] = '\0';
  172.          pIniList[lAppIdx].Key[lKeyIdx=0] = lLine;
  173.       } else {
  174.          // this is another line in the application
  175.          pIniList[lAppIdx].Key[++lKeyIdx] = lLine;
  176.       }
  177.    }
  178.    fclose(lFp);
  179.    return True;
  180. }
  181.  
  182. WriteIniFile(pFileName,pIniList) // write out ini file; false if error
  183. {
  184.    if ( !(lFp = fopen(pFileName,"wt")) )
  185.       return False;
  186.    lAppMax = GetArraySpan(pIniList);
  187.    for ( lAppIdx = 0; lAppIdx <= lAppMax; lAppIdx++ ) {
  188.       if ( defined(pIniList[lAppIdx].Key) ) {
  189.          lKeyMax = GetArraySpan(pIniList[lAppIdx].Key);
  190.          for ( lKeyIdx = 0; lKeyIdx <= lKeyMax; lKeyIdx++ ) {
  191.                if ( lLine = pIniList[lAppIdx].Key[lKeyIdx] )
  192.                   fwrite(lLine,strlen(lLine),lFp);
  193.          }
  194.          // if the last line wasn't a blank, then write a blank line
  195.          if ( !lLine  || (lLine[0] && lLine[0] != '\n') )
  196.             fprintf(lFp,"\n");
  197.       }
  198.    }
  199.    fclose(lFp);
  200.    return True;
  201. }
  202.  
  203.